package com.yonyou.pmclouds.project.processor;

import com.yonyou.pmclouds.basecom.bp.processor.MultiProcessor;
import com.yonyou.pmclouds.basecom.bp.processor.SingleProcessor;
import com.yonyou.pmclouds.basecom.bp.template.SingleDeleteBpTemplate;
import com.yonyou.pmclouds.basecom.entity.SuperVO;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.util.BeanHelper;
import com.yonyou.pmclouds.basecom.util.CommonFieldConst;
import com.yonyou.pmclouds.project.entity.ProjectBillVO;
import com.yonyou.pmclouds.project.mapper.ProjectBillMapper;
import com.yonyou.pmclouds.project.mapper.ProjectStatisticsMapper;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


public class UpdateBillCountForDeleteProcessor<T extends SuperVO> implements SingleProcessor<T>, MultiProcessor<T> {
    private ProjectStatisticsMapper mapper;
    private ProjectBillMapper projectBillMapper;
    private String billType;

    private UpdateBillCountForDeleteProcessor(ProjectStatisticsMapper mapper, ProjectBillMapper projectBillMapper, String billType) {
        this.mapper = mapper;
        this.projectBillMapper = projectBillMapper;
        this.billType = billType;
    }


    @Override
    public void process(T vo, T orig) throws BusinessException {
        String project = (String) BeanHelper.getProperty(vo, CommonFieldConst.PK_PROJECT);
        if (StringUtils.isEmpty(project)) {
            project = (String) BeanHelper.getProperty(vo, CommonFieldConst.PKPROJECT);
        }
        mapper.decreaseBillCount(1, project, billType);
        deleteBill(vo);
    }

    private void deleteBill(T vo) throws BusinessException {
        // 删除单据
        ProjectBillVO oldBillVO = projectBillMapper.getById(vo.pkFiledValue());
        SingleDeleteBpTemplate<ProjectBillVO> deleteBpTemplate = new SingleDeleteBpTemplate<>(projectBillMapper);
        deleteBpTemplate.delete(oldBillVO);
    }

    @Override
    public void process(T[] vos, T[] origs) throws BusinessException {
        if (ArrayUtils.isEmpty(vos)) {
            return;
        }
        String project = (String) BeanHelper.getProperty(vos[0], CommonFieldConst.PK_PROJECT);
        if (StringUtils.isEmpty(project)) {
            project = (String) BeanHelper.getProperty(vos[0], CommonFieldConst.PKPROJECT);
        }
        mapper.decreaseBillCount(vos.length, project, billType);
        for (T vo : vos) {
            deleteBill(vo);
        }
    }

    @Component
    public static class UpdateBillCountForDeleteProcessorFactory<T extends SuperVO> {
        @Autowired
        private ProjectStatisticsMapper mapper;
        @Autowired
        private ProjectBillMapper projectBillMapper;

        public UpdateBillCountForDeleteProcessor<T> getProcessor(String billType) {
            return new UpdateBillCountForDeleteProcessor<>(mapper, projectBillMapper, billType);
        }
    }
}
